package com.apes.fn.scm.used.model;

import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.framework.plugin.soe.metadata.BusinessAttributeValue;
import com.apes.framework.util.ApesConst;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.product.model.Classify;
import com.apes.scm.masterdata.product.model.Product;
import com.apes.scm.masterdata.product.model.ProductUom;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;


/**
 * 功能：旧件入库单明细
 *
 * @author BieBi
 * @create 2018-9-4 15:17:04
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = "war_used_store_item")
public class UsedStoreItem extends Model {
    public static final String CONST_TABLE_NAME = "war_used_store_item";

    @Id
    @Column(name = "id")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "编码")
    private String id;

    @NotNull
    @Comment("入库单号")
    @ManyToOne(optional = false)
    @JoinColumn(name = "used_store_id", referencedColumnName = "id", nullable=false)
    private UsedStore usedStore;  //入库单号

    @Comment("公司")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id")
    private PartyRole company; //公司编码

    @Comment("旧件")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "used_id", referencedColumnName = "id")
    private Used used;  // 旧件编码

    /***
     * 展示旧件单位
     */
    @Transient
    private String uom;

    public String getUom() {
        if (used == null) return null;
        return used.getUom();
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "product_id", referencedColumnName = "id")
    @Comment("商品")
    private Product product; //商品

    @Transient
    private Classify classify;

    public Classify getClassify(){
        return product != null ? product.getClassify() : null;
    }

    @Comment("来源数量")
    @Column(name = "qty_source", columnDefinition = ApesConst.FIELD_16_4)
    private double qytSource;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "source_uom_id", referencedColumnName = "id")
    @Comment("来源单位")
    private ProductUom sourceUom;

    @Comment("入库数量")
    @Column(name = "qty_store", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    private double qtyStore = 0; // 入库数量

    @States(name = "入库方式", values = {
            @State(id = "normal", name = "正常"),
            @State(id = "pickUp", name = "自提")})
    @ManyToOne(fetch = FetchType.LAZY)
    @Comment("入库方式")
    @JoinColumn(name = "store_method", referencedColumnName = "id")
    private BusinessAttributeValue storeMethod;

    @Comment("备注")
    @Column(name = "remark", length = 500)
    private String remark; //摘要

    @Comment("来源行号")
    @Column(name = "origin_line")
    private String originLine;  //来源行号

}